package com.ytjj.qmyx.mall.controller.h5;


import com.google.common.collect.Maps;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.qmyx.mall.model.UsersRealName;
import com.ytjj.qmyx.mall.model.request.UsersRealNameRequest;
import com.ytjj.qmyx.mall.service.h5.H5RealNameAuthService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 跨进电商实名认证相关业务
 */
@RestController
@RequestMapping("/h5/relNameAuth")
public class H5RealNameAuthController extends BaseController {

    @Autowired
    private H5RealNameAuthService h5RealNameAuthService;

    /**
     * 保存
     * @return
     */
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody UsersRealNameRequest realName){
        UsersResponse usersResponse = super.getCurrentUser();
        realName.setUserId(usersResponse.getId());
        int id = h5RealNameAuthService.save(realName);
        Map<String, Integer> map = Maps.newHashMap();
        map.put("id",id);
        return CommonResult.success(map);
    }

    /**
     * 查询列表
     * @return
     */
    @GetMapping("/findList")
    public CommonResult findList(){
        UsersResponse usersResponse = super.getCurrentUser();
        return CommonResult.success(h5RealNameAuthService.findList(usersResponse.getId()));
    }

    /**
     * 设置默认订购人
     * @param realName
     * @return
     */
    @PostMapping("/setDefaultSelect")
    public CommonResult setDefaultSelect(@RequestBody UsersRealName realName){
        Assert.notNull(realName.getId(),"id不能为空");
        UsersResponse usersResponse = super.getCurrentUser();
        realName.setUserId(usersResponse.getId());
        h5RealNameAuthService.setDefaultSelect(realName);
        return CommonResult.success();
    }

    /**
     * 查询认证人详细
     * @return
     */
    @GetMapping("/getById")
    public CommonResult getById(Integer id){
        return CommonResult.success(h5RealNameAuthService.getById(id));
    }

    /**
     * 修改
     * @param realName
     * @return
     */
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody UsersRealNameRequest realName){
        Assert.notNull(realName.getId(),"id不能为空");
        h5RealNameAuthService.update(realName);
        return CommonResult.success();
    }

    /**
     *  删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult delete(@PathVariable("id") Integer id){
        Assert.notNull(id,"id不能为空");
        h5RealNameAuthService.delete(id);
        return CommonResult.success();
    }
}
